package br.com.mtg.movethegarbage.screens;

import android.util.Log;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.R;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.Button;
import br.com.mtg.movethegarbage.impl.ArrowButton;

public class TutorialScreen extends Screen
{
	Button yes, no;
	Button buttonTutorial1, buttonTutorial2, buttonTutorial3, buttonTutorial4;
	Camera2D cam;
	SpriteManager spriteManager;
	Button right, left, up, down;

	public TutorialScreen(Game _game)
	{
		super(_game);

		multiplier = game.getGraphics().getMultiplier();
		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		cam = new Camera2D(game.getGraphics());

		spriteManager = new SpriteManager(game.getGraphics(), 1);

		Assets.font.setScale(1.0F * multiplier);

		yes = new Button(width / 2 - (50 * multiplier + 10 * multiplier), height - (50 * multiplier + 20 + Assets.font.getTextHeight()), 50 * multiplier,
				50 * multiplier);
		no = new Button(width / 2 + 10 * multiplier, height - (50 * multiplier + 20 + Assets.font.getTextHeight()), 50 * multiplier, 50 * multiplier);
		yes.setButtonSprite(Assets.spriteYes);
		no.setButtonSprite(Assets.spriteNo);
		
		right = new ArrowButton(width - 40 * multiplier, (height - 40 * multiplier) / 2, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_RIGHT);
		left = new ArrowButton(0, (height - 20 * multiplier) / 2, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_LEFT);
		up = new ArrowButton((width - 40 * multiplier) / 2, height - 40 * multiplier, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_UP);
		down = new ArrowButton((width - 40 * multiplier) / 2, 0, 40 * multiplier, 40 * multiplier, ArrowButton.DIRECTION_DOWN);

		right.setButtonSprite(Assets.buttonArrowGray);
		left.setButtonSprite(Assets.buttonArrowGray);
		up.setButtonSprite(Assets.buttonArrowGray);
		down.setButtonSprite(Assets.buttonArrowGray);
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();
		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				yes.handleEvent(e, game);
				no.handleEvent(e, game);
			}
			input.getTouchEvents().clear();
		}

		if (yes.wasPressed())
		{
			game.setScreen(new GameScreen(game, 0));
		}
		else if (no.wasPressed())
		{
			try
			{
				game.saveProgress(1);
			}
			catch (Exception e)
			{
				Log.e("TutorialScreen", "Error saving progress");
			}
			game.setScreen(new GameScreen(game, 1));
		}
	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(Assets.tex_backgroundScreenRedMain);
		spriteManager.drawSprite(0, 0, width, height, Assets.background);
		spriteManager.endDraw();

		Assets.font.setScale(1.0F * multiplier);
		float fontWidth = Assets.font.getTextLength(game.getStringFromResource(R.string.tutorial));
		Assets.font.printAt(game.getStringFromResource(R.string.tutorial), (width - fontWidth) / 2, height - Assets.font.getTextHeight());

		Assets.font.setScale(0.8F);
		String[] words = game.getStringFromResource(R.string.tutorialHint).split(";");
		float stringWidth;
		for (int i = 0; i < words.length; i++)
		{
			stringWidth = Assets.font.getTextLength(words[i]);
			Assets.font.printAt(words[i], (width - stringWidth) / 2, (words.length - (i + 1)) * Assets.font.getTextHeight() + 40 * multiplier);
		}

		yes.draw(game.getGraphics());
		no.draw(game.getGraphics());
		
		right.draw(game.getGraphics());
		left.draw(game.getGraphics());
		up.draw(game.getGraphics());
		down.draw(game.getGraphics());
	}

	@Override
	public void pause()
	{

	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
	}

	@Override
	public void dispose()
	{

	}

}
